import Vue from 'vue'
import VueRouter from 'vue-router'
import routes from '@/router/routes'
import '@/utils/vant'
import { getToken } from '@/utils/storage'

Vue.use(VueRouter)

const router = new VueRouter({
  routes
})

// 白名单
const whiteList = ['/login', '/register', '/main', '/list', '/news', '/profile']
// 全局前置守卫
router.beforeEach((to, from, next) => {
  console.log(to)
  console.log(from)
  const token = getToken()
  if (token) {
    next()
  } else {
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }
})

export default router
